package zhibi.cms.controller.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import zhibi.cms.domain.Menu;
import zhibi.cms.extra.base.AdminBaseController;
import zhibi.cms.extra.context.Constant;
import zhibi.cms.extra.pojo.MenuPo;
import zhibi.cms.service.MenuService;
import zhibi.commons.exception.MessageException;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping(Constant.SYS_PREFIX + "/admin/menu")
public class CmsAdminMenuController extends AdminBaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 菜单列表
     *
     * @param model
     * @param page
     * @return
     */
    @RequestMapping("list")
    public String list(Model model, Page page, MenuPo menuPo) {
        Example example = Example.getInstance()
                .addParam("m.name", menuPo.getName(), ExampleType.Operation.LIKE)
                .addOrder("m.sort", ExampleType.OrderType.DESC)
                .addOrder("m.pid", ExampleType.OrderType.DESC);
        PageInfo<MenuPo> pageInfo = menuService.selectPoByExample(example, page);
        setModelAttribute(model, pageInfo);
        return Constant.SYS_PREFIX + "/menu/list";
    }

    /**
     * 到添加页面
     *
     * @param model
     * @return
     */
    @GetMapping("add")
    public String add(Model model) {
        List<Menu> menuList = menuService.selectAll();
        model.addAttribute("menuList", menuList);
        return Constant.SYS_PREFIX + "/menu/add";
    }

    /**
     * 添加菜单
     *
     * @return
     */
    @PostMapping("add")
    public String add(Menu menu, MultipartFile file) {
        if (menu.getPid() != null && !Objects.equals(menu.getPid(), "")) {
            Menu pMenu = menuService.selectByPrimaryKey(menu.getPid());
            if (null == pMenu) throw new MessageException("上级菜单不存在");
        }
        if (file != null && !file.isEmpty()) {
            menu.setShowpic(saveFile(file, "MENU"));
        }
        menu.setStatus(1);
        menu.setAddtime(new Date());
        menuService.insertSelective(menu);
        return redirect("list");
    }

    /**
     * 菜单详情
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("edit/{id}")
    public String edit(Model model, @PathVariable String id) {
        Menu menu = menuService.selectByPrimaryKey(id);
        if (null == menu) throw new MessageException("该菜单不存在");
        List<Menu> menuList = menuService.selectAll();
        model.addAttribute("menuList", menuList);
        model.addAttribute("menu", menu);
        return Constant.SYS_PREFIX + "/menu/edit";
    }

    /**
     * 更新菜单
     *
     * @param menu
     * @return
     */
    @PostMapping("update")
    public String update(Menu menu, MultipartFile file) {
        if (menu.getPid() != null && !Objects.equals(menu.getPid(), "")) {
            Menu pMenu = menuService.selectByPrimaryKey(menu.getPid());
            if (null == pMenu) throw new MessageException("上级菜单不存在");
        }
        if (file != null && !file.isEmpty()) {
            menu.setShowpic(saveFile(file, "MENU"));
        }
        menuService.updateByPrimaryKeySelective(menu);
        return refresh();
    }

    /**
     * 删除菜单
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable String id) {
        Menu menu = menuService.selectByPrimaryKey(id);
        if (null == menu) throw new MessageException("该菜单不存在");
        List<Menu> menuList = menuService.selectByPid(menu.getId());
        if (menuList != null && menuList.size() > 0) throw new MessageException("该菜单下面存在子菜单，不能删除");
        menuService.delete(menu);
        return refresh();
    }

    /**
     * 更新菜单状态
     *
     * @param id
     * @return
     */
    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable String id) {
        Menu menu = menuService.selectByPrimaryKey(id);
        if (null == menu) throw new MessageException("该菜单不存在");
        menu.setStatus((menu.getStatus() + 1) % 2);
        menuService.updateByPrimaryKeySelective(menu);
        return refresh();
    }
}
